﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using JC_DirectBroadcast.Core;
using JC_DirectBroadcast.Repository.Interface;

namespace JC_DirectBroadcast.Repository
{
    public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class,  new ()
    {
        private readonly SqlSugarClient _db;

        public SimpleClient<TEntity> Context => new SimpleClient<TEntity>(_db); //用来处理T表的常用操作

        public BaseRepository(SqlSugarClient db)
        {
            this._db = db;
        }


        public TEntity GetById(object key)
        {
            return this.Context.GetById(key);
        }

        public bool IsExist(Expression<Func<TEntity, bool>> wherExpression)
        {
            return this.Context.IsAny(wherExpression);
        }

        public TEntity GetSingle(Expression<Func<TEntity, bool>> wherExpression)
        {
            return this.Context.GetSingle(wherExpression);
        }

        public List<TEntity> GetPageList(Expression<Func<TEntity, bool>> whereExpression, PageModel page, Expression<Func<TEntity, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
        {
            return this.Context.GetPageList(whereExpression, page, orderByExpression, orderByType);
        }

        public List<TEntity> GetList(Expression<Func<TEntity, bool>> whereExpression=null)
        {
            return this.Context.GetList(whereExpression);
        }

        public int Count(Expression<Func<TEntity, bool>> whereExpression)
        {
            return this.Context.Count(whereExpression);
        }

        public bool Insert(TEntity entity)
        {
            return this.Context.Insert(entity);
        }

        public int InsertReturnIdentity(TEntity entity)
        {
            return this.Context.InsertReturnIdentity(entity);
        }

        public bool InsertRange(TEntity[] entities)
        {
            return this.Context.InsertRange(entities);
        }

        public bool InsertRange(List<TEntity> entities)
        {
            return this.Context.InsertRange(entities);
        }

        public bool Update(TEntity entity)
        {
            return this.Context.Update(entity);
        }

        public bool UpdateRange(TEntity[] entities)
        {
            return this.Context.UpdateRange(entities);
        }

        public bool UpdateRange(List<TEntity> entities)
        {
            return this.Context.UpdateRange(entities);
        }

        public bool Update(Expression<Func<TEntity, TEntity>> columns, Expression<Func<TEntity, bool>> whereExpression)
        {
            return this.Context.Update(columns, whereExpression);
        }

        public bool Delete(TEntity entity)
        {
            return this.Context.Delete(entity);
        }

        public bool Delete(Expression<Func<TEntity, bool>> whereExpression)
        {
            return this.Context.Delete(whereExpression);
        }

        public bool DeleteById(object id)
        {
            return this.Context.DeleteById(id);
        }

        public bool DeleteByIds(object[] ids)
        {
            return this.Context.DeleteByIds(ids);
        }
    }
}
